package com.yifi.common.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.model.AjaxResult;
import com.yifi.common.vo.TableDataInfo;

public class InitController extends Controller {

	protected TableDataInfo getDataTable(Page<?> list) {
		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(0);
		rspData.setRows(list.getList());
		rspData.setTotal(list.getTotalRow());
		return rspData;
	}

	protected TableDataInfo getDataTable(String sqlSelect, String sqlExcept) {
		int pageNum = getParaToInt("pageNum", 1);
		int pageSize = getParaToInt("pageSize", 20);
		return getDataTable(pageNum, pageSize, sqlSelect, sqlExcept);
	}

	protected TableDataInfo getDataTable(String sqlSelect, String sqlExcept, Object... paras) {
		int pageNum = getParaToInt("pageNum", 1);
		int pageSize = getParaToInt("pageSize", 20);
		return getDataTable(pageNum, pageSize, sqlSelect, sqlExcept, paras);
	}

	protected TableDataInfo getDataTable(int pageNum, int pageSize, String sqlSelect, String sqlExcept) {
		Object[] paras = new Object[] {};
		return getDataTable(pageNum, pageSize, sqlSelect, sqlExcept, paras);
	}

	/**
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param sqlSelect
	 * @param sqlExcept
	 * @param paras     多参数
	 * @return
	 */
	protected TableDataInfo getDataTable(int pageNum, int pageSize, String sqlSelect, String sqlExcept,
			Object... paras) {
		System.out.println(sqlSelect + sqlExcept);
		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(0);
		try {
			Page<Record> pageR = Db.paginate(pageNum, pageSize, sqlSelect, sqlExcept, paras);

			rspData.setRows(pageR.getList());
			rspData.setTotal(pageR.getTotalRow());
			rspData.setTotalPage(pageR.getTotalPage());
		} catch (Exception e) {
			System.out.println(e.toString());
			// TODO: handle exception
		}
		return rspData;
	}

	protected TableDataInfo getDataTable(String sql) {
		List<Record> list = Db.find(sql);
		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(0);
		rspData.setRows(list);
		rspData.setTotal(list.size());
		rspData.setTotalPage(1);
		return rspData;
	}

	protected TableDataInfo getDataPage(String sqlSelect, String sqlExcept) {
		int page = getParaToInt("page", 1);
		int rows = getParaToInt("rows", 20);
		return getDataPage(page, rows, sqlSelect, sqlExcept);
	}

	protected TableDataInfo getDataPage(String sqlSelect, String sqlExcept, Object... paras) {
		int page = getParaToInt("page", 1);
		int rows = getParaToInt("rows", 20);
		return getDataPage(page, rows, sqlSelect, sqlExcept, paras);
	}

	protected TableDataInfo getDataPage(int page, int rows, String sqlSelect, String sqlExcept) {
		Object[] paras = new Object[] {};
		return getDataPage(page, rows, sqlSelect, sqlExcept, paras);
	}

	/**
	 * 
	 * @param page
	 * @param rows
	 * @param sqlSelect
	 * @param sqlExcept
	 * @param paras     多参数
	 * @return
	 */
	protected TableDataInfo getDataPage(int page, int rows, String sqlSelect, String sqlExcept, Object... paras) {
		Page<Record> pageR = Db.paginate(page, rows, sqlSelect, sqlExcept, paras);
		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(0);
		rspData.setRows(pageR.getList());
		rspData.setTotal(pageR.getTotalRow());
		return rspData;
	}

	@Deprecated
	protected Page<Record> getTablePage(String sqlSelect, String sqlExcept) {
		int page = getParaToInt("page", 1);
		int rows = getParaToInt("rows", 20);

		return getTablePage(page, rows, sqlSelect, sqlExcept);
	}

	@Deprecated
	protected Page<Record> getTablePage(int pageNum, int pageSize, String sqlSelect, String sqlExcept) {
		Page<Record> pageR = Db.paginate(pageNum, pageSize, sqlSelect, sqlExcept);
		return pageR;
	}

	protected List<Record> exportXlsx(String sqlSelect, String sqlExcept) {
		String sql = sqlSelect + sqlExcept;
		List<Record> list = Db.find(sql);
		return list;
	}

	/**
	 * 响应返回结果
	 *
	 * @param rows 影响行数
	 * @return 操作结果
	 */
	protected AjaxResult toAjax(int rows) {
		return rows > 0 ? success() : error();
	}

	/**
	 * 响应返回结果
	 *
	 * @param result 结果
	 * @return 操作结果
	 */
	protected AjaxResult toAjax(boolean result) {
		return result ? success() : error();
	}

	/**
	 * 返回成功
	 */
	public AjaxResult success() {
		return AjaxResult.success();
	}

	/**
	 * 返回失败消息
	 */
	public AjaxResult error() {
		return AjaxResult.error();
	}

	/**
	 * 返回成功消息
	 */
	public AjaxResult success(String message) {
		return AjaxResult.success(message);
	}

	public AjaxResult success(Object data) {
		return AjaxResult.success(data);
	}

	/**
	 * 返回失败消息
	 */
	public AjaxResult error(String message) {
		return AjaxResult.error(message);
	}

	public AjaxResult error(String message, Object data) {
		return AjaxResult.error(message, data);
	}

	/**
	 * 返回错误码消息
	 */
	public AjaxResult error(AjaxResult.Type type, String message) {
		return new AjaxResult(type, message);
	}

	/**
	 * 返回错误码消息
	 */
	public AjaxResult error(AjaxResult.Type type, String message, Object data) {
		return new AjaxResult(type, message, data);
	}

	public Map<String, Object> toResult(boolean success, String message, Object data) {
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("success", success);
		rs.put("msg", message);
		rs.put("data", data);
		return rs;
	}

	// 成功
	public Map<String, Object> toSuccess(String message, Object data) {
		return toResult(true, message, data);
	}

	public Map<String, Object> toSuccess(String message) {
		return toResult(true, message, null);
	}

	// 失败
	public Map<String, Object> toError(String message, Object data) {
		return toResult(false, message, data);
	}

	public Map<String, Object> toError(String message) {
		return toResult(false, message, null);
	}
}
